//
//  CalenderToos.swift
//  summaryGraph
//
//  Created by jieliapp on 2017/2/23.
//  Copyright © 2017年 飞天江郎. All rights reserved.
//

import UIKit

class CalenderToos: NSObject {
    
    
   /// 计算日期差值（计算从开始到现在查了多少月份的）
   ///
   /// - Parameter fromDate: 最初的日期
   /// - Returns: 相差了多少个月
   class func calculationWithDate(fromDate:Date) -> Int{
    
    let fromatter = DateFormatter()
    fromatter.dateFormat = "yyyy-MM"
    fromatter.timeZone = NSTimeZone.init(forSecondsFromGMT: 8*3600) as TimeZone!
    let fromString = fromatter.string(from: fromDate)
    
    let nowDate = NSDate() as Date
    let nowString = fromatter.string(from: nowDate)
    
    let fromArr = fromString.components(separatedBy: "-")
    let nowArr = nowString.components(separatedBy: "-")
    
    var nowYear = Int(nowArr[0])!
    
    var tagMonth = 0
    
    if Int(nowArr[1])! - Int(fromArr[1])! < 0{
    
        nowYear = nowYear - 1
        let k = Int(nowArr[1])!+12
        tagMonth = k - Int(fromArr[1])!
        
    }else{
    
        tagMonth = Int(nowArr[1])! - Int(fromArr[1])!
    }
    
    let yearInt:Int = (nowYear - Int(fromArr[0])!)*12
    
    tagMonth = tagMonth + yearInt
    
    
    return tagMonth
        
    }
    
    
    /// 输入日期返回对应的星期（周日 == 1，周一 == 2）
    ///
    /// - Parameter fromDate: 日期
    /// - Returns: 星期几
    class func calculationWeekDayWithDate(fromDate:Date) -> Int{
        
        let componets = NSCalendar.autoupdatingCurrent.component(.weekday, from: fromDate)
    
        return componets

    }
    
    
    
    /// 返回月份里的天数
    ///
    /// - Parameter fromDate: 日期
    /// - Returns: 天
    class func calculationForMonthHaveDays(fromDate:Date) -> Int{
    
        let daysInLastMonth = NSCalendar.current.range(of: .day, in: .month, for: fromDate)
        
        return daysInLastMonth!.count
    
    }
    
    
    
    /// 返回月里面的天数数组
    ///
    /// - Parameter date: 日期
    /// - Returns: 天数数组（含kong假数组）
    class func monthDayArrayFromDate(date:Date) -> Array<String> {
        
        var monthDay:Array<String> = []
        
        let month = CalenderToos.calculationForMonthHaveDays(fromDate: date)
        
        let index = CalenderToos.calculationWeekDayWithDate(fromDate: date) - 1
        
        for _ in 0..<index{
            
            monthDay.append("kong")
            
        }
        
        for i in 1...month{
            
            monthDay.append(String(i))
            
            
        }
        
        return monthDay
        
    }
    
    
    /// 传入一个开始时间点，返回该时间点以后的月份数组
    ///
    /// - Parameter fromDate: 开始日期
    /// - Returns: 日期数组
    class func monthArrayFromDate(fromDate:Date) ->Array<Date>{
        
        let index = self.calculationWithDate(fromDate: fromDate)
        
        let fromatter = DateFormatter()
        fromatter.dateFormat = "yyyy-MM-dd"
        fromatter.timeZone = NSTimeZone.init(forSecondsFromGMT: 8*3600) as TimeZone!
        
        if index == 0 {
            
            return [fromDate]
            
        }else{
            
            var calenderArr:Array<Date>! = []
            
            let sinceStr = fromatter.string(from: fromDate)
            let sinceArr = sinceStr.components(separatedBy: "-")
            
            var monthMark:Int! = Int(sinceArr[1])
            var yearMark:Int! = Int(sinceArr[0])
            
            for _ in 0...index{
                
               
                if monthMark == 13{
                    yearMark = yearMark + 1
                    monthMark = 1
                }
                
                let tagString = String(yearMark) + "-" + String(monthMark) + "-1"
                let tagDate = fromatter.date(from: tagString)
                
                monthMark = monthMark! + 1
                calenderArr.append(tagDate!)
            }
            
            return calenderArr
            
        }
    }
    
    
    

}



